package com.ronsicecream.pos.master.supplier;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.ronsicecream.pos.entities.PosMasterSupplier;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class Supplier extends Page {

    private Field supplierId;
    private Field supplierCode;
    private Field supplierName;
    private Field supplierAddress;
    private Field supplierPhone;
    private Field supplierContact;

    public Supplier() {
        setTheClass(PosMasterSupplier.class);

        supplierId = new Field();
        supplierId.setName("supplierId");
        supplierId.setCaption("id");
        supplierId.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        supplierId.setInputProperties(" size=\"10\"");
        getFields().add(supplierId);

        supplierCode = new Field();
        supplierCode.setName("supplierCode");
        supplierCode.setCaption("code");
        supplierCode.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        supplierCode.setInputProperties(" size=\"14\"");
        supplierCode.setRequired(true);
        supplierCode.setInputCase(Field.CASE_UPPER);
        getFields().add(supplierCode);

        supplierName = new Field();
        supplierName.setName("supplierName");
        supplierName.setCaption("name");
        supplierName.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        supplierName.setInputProperties(" size=\"30\"");
        supplierName.setRequired(true);
        supplierName.setInputCase(Field.CASE_UPPER);
        getFields().add(supplierName);

        supplierAddress = new Field();
        supplierAddress.setName("supplierAddress");
        supplierAddress.setCaption("address");
        supplierAddress.setListProperties(" align=\"left\" width=\"35%\" nowrap");
        supplierAddress.setInputProperties(" rows=\"5\"cols=\"50\"");
        supplierAddress.setInputType(Field.INPUT_TEXT_AREA);
        supplierAddress.setRequired(true);
        supplierAddress.setInputCase(Field.CASE_UPPER);
        getFields().add(supplierAddress);

        supplierPhone = new Field();
        supplierPhone.setName("supplierPhone");
        supplierPhone.setCaption("phone");
        supplierPhone.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        supplierPhone.setInputProperties(" size=\"20\"");
        supplierPhone.setRequired(true);
        getFields().add(supplierPhone);

        supplierContact = new Field();
        supplierContact.setName("supplierContact");
        supplierContact.setCaption("contact");
        supplierContact.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        supplierContact.setInputProperties(" size=\"16\"");
        supplierContact.setRequired(true);
        supplierContact.setInputCase(Field.CASE_UPPER);
        getFields().add(supplierContact);
        fillFields();

        getFieldsUnique().add(new Field[]{supplierCode});
        getFieldsAdd().remove(supplierId);
        getFieldsEdit().remove(supplierId);
        getFieldsList().remove(supplierId);

        setExportList(getClass().getResourceAsStream("SupplierList.jasper"));
        setExport(getClass().getResourceAsStream("Supplier.jasper"));
        getController().setOrder(supplierName);
//        setEditActionRedirect("m=" + getMenu().getMenuName() + "&a=" + Action.EDIT.getName() + "&id=" + Utility.decode(Utility.getPostParameter(this, "id")));
    }

    @Override
    public void beforeList() {
        if (!isAdministrator()) {
            supplierName.setListProperties(" align=\"left\" width=\"90%\" nowrap");
            getFieldsList().remove(supplierAddress);
            getFieldsList().remove(supplierContact);
            getFieldsList().remove(supplierPhone);
        } else {
            supplierName.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        }
    }

    private boolean isAdministrator() {
        try {
            EntityManager em = getEntityManager();
            Query query = em.createQuery("SELECT t FROM MasterUserGroup t WHERE t.userGroupUserId.userId=" + getCurrentUser().getUserId() + " AND t.userGroupGroupId.groupId=1");
            query.getSingleResult();
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    @Override
    public void beforeView() {
        beforeList();
    }
    
     @Override
    public String doAction() {
        setEditActionRedirect("m=" + getMenu().getMenuName() + "&a=" + Action.EDIT.getName() + "&id=" + Utility.decode(Utility.getPostParameter(this, "id")));
        return super.doAction();
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "supplier"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
